package xxl.robotlegs.base 
{
	import flash.utils.Dictionary;
	import xxl.robotlegs.core.IConfig;
	import xxl.robotlegs.core.IContext;
	import xxl.robotlegs.core.IInjector;
	import xxl.robotlegs.core.ILogger;
	import xxl.robotlegs.core.IMatcher;
	/**
	 * ...
	 * @author XXL
	 */
	public class ConfigManager 
	{
		private const _objectProcessor:ObjectProcessor = new ObjectProcessor();
		private const _configs:Dictionary = new Dictionary();
		private const _queue:Array = [];
		private var _injector:IInjector;
		private var _logger:ILogger;
		private var _initialized:Boolean;
		
		public function ConfigManager(context:IContext) 
		{
			_injector = context.injector;
			_logger = context.getLogger(this);
			addConfigHandler(new ClassMatcher(), handleClass);
			addConfigHandler(new ObjectMatcher(), handleObject);
			//event
		}
		
		public function addConfig(config:Object):void
		{
			if (!_configs[config])
			{
				_configs[config] = true;
				_objectProcessor.processObject(config);
			}
		}
		
		public function addConfigHandler(matcher:IMatcher, handler:Function):void
		{
			_objectProcessor.addObjectHandler(matcher, handler);
		}
		
		//----------private
		
		private function initialize():void
		{
			if (!_initialized)
			{
				_initialized = true;
				processQueue();
			}
		}
		
		private function handleClass(type:Class):void
		{
			if (_initialized)
			{
				_logger.debug("已经初始化了。Class：{0}", [type]);
				processClass(type);
			}
			else
			{
				_logger.debug("还没有初始化，追加到队列中。Class：{0}", [type]);
				_queue.push(type);
			}
		}
		
		private function handleObject(object:Object):void
		{
			if (_initialized)
			{
				_logger.debug("已经初始化了。Object：{0}", [object]);
				processObject(object);
			}
			else
			{
				_logger.debug("还没有初始化，追加到队列中。Object：{0}", [object]);
				_queue.push(object);
			}
		}
		
		private function processQueue():void 
		{
			const len:int = _queue.length;
			for (var i:int = 0; i < len; i++ )
			{
				var config:Object = _queue[i];
				if (config is Class)
				{
					_logger.debug("初始化中。。。Class：{0}", [config]);
					processClass(config as Class);
				}
				else
				{
					_logger.debug("初始化中。。。Object：{0}", [config]);
					processObject(config);
				}
			}
			_queue.length = 0;
		}
		
		private function processClass(type:Class):void
		{
			const config:IConfig = _injector.getOrCreateNewInstance(type) as IConfig;
			config && config.configure();
		}
		
		private function processObject(object:Object):void
		{
			_injector.injectInto(object);
			const config:IConfig = object as IConfig;
			config && config.configure();
		}
		
	}

}
import xxl.robotlegs.core.IMatcher;

class ClassMatcher implements IMatcher
{
	public function ClassMatcher() 
	{
		
	}
	
	/* INTERFACE xxl.robotlegs.core.IMatcher */
	
	public function matches(item:*):Boolean 
	{
		return item is Class;
	}
}

class ObjectMatcher implements IMatcher
{
	public function ObjectMatcher() 
	{
		
	}
	
	/* INTERFACE xxl.robotlegs.core.IMatcher */
	
	public function matches(item:*):Boolean 
	{
		return item is Class == false;
	}
}